package com.hongxun.net;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.hongxun.budget.greendao.APCostEntityDao;
import com.hongxun.budget.greendao.APCriteriaEntityDao;
import com.hongxun.budget.greendao.APEstimateEntityDao;
import com.hongxun.budget.greendao.APOtherEntityDao;
import com.hongxun.budget.greendao.APRangeEntityDao;
import com.hongxun.budget.greendao.APWireEntityDao;
import com.hongxun.budget.greendao.DCrossCostEntityDao;
import com.hongxun.budget.greendao.DDeviceBuyEntityDao;
import com.hongxun.budget.greendao.DDeviceOldEntityDao;
import com.hongxun.budget.greendao.DDrawLineEntityDao;
import com.hongxun.budget.greendao.DMaterialBuyEntityDao;
import com.hongxun.budget.greendao.DMaterialCostEntityDao;
import com.hongxun.budget.greendao.DMaterialModelEntityDao;
import com.hongxun.budget.greendao.DMaterialModelItemEntityDao;
import com.hongxun.budget.greendao.DMaterialOldEntityDao;
import com.hongxun.budget.greendao.DOtherCostEntityDao;
import com.hongxun.budget.greendao.DPictureEntityDao;
import com.hongxun.budget.greendao.DPointEntityDao;
import com.hongxun.budget.greendao.DWorkContentEntityDao;
import com.hongxun.budget.greendao.DaoSession;
import com.hongxun.budget.utils.GreenDaoManager;
import com.hongxun.greenentity.tk.APBaseEntity;
import com.hongxun.greenentity.tk.APCostEntity;
import com.hongxun.greenentity.tk.APCriteriaEntity;
import com.hongxun.greenentity.tk.APEstimateEntity;
import com.hongxun.greenentity.tk.APOtherEntity;
import com.hongxun.greenentity.tk.APRangeEntity;
import com.hongxun.greenentity.tk.APWireEntity;
import com.hongxun.greenentity.tk.DCrossCostEntity;
import com.hongxun.greenentity.tk.DDeviceBuyEntity;
import com.hongxun.greenentity.tk.DDeviceOldEntity;
import com.hongxun.greenentity.tk.DDrawLineEntity;
import com.hongxun.greenentity.tk.DMaterialBuyEntity;
import com.hongxun.greenentity.tk.DMaterialCostEntity;
import com.hongxun.greenentity.tk.DMaterialModelEntity;
import com.hongxun.greenentity.tk.DMaterialModelItemEntity;
import com.hongxun.greenentity.tk.DMaterialOldEntity;
import com.hongxun.greenentity.tk.DOtherCostEntity;
import com.hongxun.greenentity.tk.DPictureEntity;
import com.hongxun.greenentity.tk.DPointEntity;
import com.hongxun.greenentity.tk.DWorkContentEntity;
import com.hongxun.interfaces.NovaCallback;

import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2018/8/31.
 */

public class ProjectNet extends BaseNet {

    //项目列表
    public static void projectInfo(Context context, NovaCallback c) {
        getNovate(context, "获取项目列表").rxPost(Contants.projectInfo, getParams(context), new Callback(999, c, d));
    }

    //下载项目
    public static void downProject(Context context, String uuid, NovaCallback c) {
        Map<String, Object> map = getParams(context);
        map.put("uuid", uuid);
        getNovate(context, "正在下载项目").rxPost(Contants.downProject, map, new Callback(888, c, d));
    }

    //更新项目信息(基本信息)
    public static void updateProject(Context context, String uuid, NovaCallback c) {
        DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
        Map<String, Object> map = getParams(context);
        APBaseEntity apBaseEntity = session.getAPBaseEntityDao().load(uuid);
        List<APCostEntity> apCostEntities = session.getAPCostEntityDao().queryBuilder()
                .where(APCostEntityDao.Properties.PGuid.eq(apBaseEntity.getGuid())
                        , APCostEntityDao.Properties.PCDatastate.notEq("已同步")).list();
        List<APCriteriaEntity> apCriteriaEntities = session.getAPCriteriaEntityDao().queryBuilder()
                .where(APCriteriaEntityDao.Properties.PGuid.eq(apBaseEntity.getGuid())
                        , APCriteriaEntityDao.Properties.PCDatastate.notEq("已同步")).list();
        List<APEstimateEntity> apEstimateEntities = session.getAPEstimateEntityDao().queryBuilder()
                .where(APEstimateEntityDao.Properties.PGuid.eq(apBaseEntity.getGuid())
                        , APEstimateEntityDao.Properties.PEDatastate.notEq("已同步")).list();
        List<APOtherEntity> apOtherEntities = session.getAPOtherEntityDao().queryBuilder()
                .where(APOtherEntityDao.Properties.PGuid.eq(apBaseEntity.getGuid())
                        , APOtherEntityDao.Properties.PODatastate.notEq("已同步")).list();
        List<APRangeEntity> apRangeEntities = session.getAPRangeEntityDao().queryBuilder()
                .where(APRangeEntityDao.Properties.PGuid.eq(apBaseEntity.getGuid())
                        , APRangeEntityDao.Properties.PRDatastate.notEq("已同步")).list();
        List<APWireEntity> apWireEntities = session.getAPWireEntityDao().queryBuilder()
                .where(APWireEntityDao.Properties.PGuid.eq(apBaseEntity.getGuid())
                        , APWireEntityDao.Properties.PWDatastate.notEq("已同步")).list();
        map.put("apBaseEntity", JSON.toJSONString(apBaseEntity));
        map.put("apCostEntities", JSON.toJSONString(apCostEntities));
        map.put("apCriteriaEntities", JSON.toJSONString(apCriteriaEntities));
        map.put("apEstimateEntities", JSON.toJSONString(apEstimateEntities));
        map.put("apOtherEntities", JSON.toJSONString(apOtherEntities));
        map.put("apRangeEntities", JSON.toJSONString(apRangeEntities));
        map.put("apWireEntities", JSON.toJSONString(apWireEntities));
        getNovate(context, "项目基础信息上传中...").rxPost(Contants.projectUpdate, map, new Callback(1, c, d));
    }

    /**
     * 上传图片
     *
     * @param context
     * @param uuid
     * @param c
     */
    public static void upPhoto(Context context, String uuid, NovaCallback c) {
        List<DPictureEntity> list = GreenDaoManager.getInstance().getmDaoSession().getDPictureEntityDao().queryBuilder().where(
                DPictureEntityDao.Properties.PGuid.eq(uuid)
                , DPictureEntityDao.Properties.PStatus.eq("储备")
                , DPictureEntityDao.Properties.PDatastate.notEq("已同步")
        ).list();
//        ZipUtils.zip(list);//先打包文件
//        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data; charset=utf-8"), new File(ZipUtils.zipFilePath));
//        RequestBody requestBody = new MultipartBody.Builder()
//                .setType(MultipartBody.FORM)
//                .addFormDataPart("fileName", "zip", requestFile)
//                .addFormDataPart("photo", JSON.toJSONString(list))
//                .build();
//        getNovate(context, "图片信息上传中...").rxUploadWithBody("", Contants.photoUpload, requestBody, new Callback(2, c, d));
        getDialog(context, "图片信息上传中");
        upPic(context, list, c, 2);
    }

    //上传点位信息
    public static void projectPoint(Context context, String uuid, NovaCallback c) {
        Map<String, Object> map = getParams(context);
        DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
        List<DPointEntity> dPointEntities = session.getDPointEntityDao().queryBuilder().where(DPointEntityDao.Properties.PGuid.eq(uuid)
                , DPointEntityDao.Properties.PPDatastate.notEq("已同步")).list();
        List<DDrawLineEntity> dDrawLineEntities = session.getDDrawLineEntityDao().queryBuilder().where(DDrawLineEntityDao.Properties.PGuid.eq(uuid)
                , DDrawLineEntityDao.Properties.DLDatastate.notEq("已同步")).list();
        List<DCrossCostEntity> dCrossCostEntities = session.getDCrossCostEntityDao().queryBuilder().where(DCrossCostEntityDao.Properties.PGuid.eq(uuid)
                , DCrossCostEntityDao.Properties.CCDatastate.notEq("已同步")).list();
        map.put("dPointEntities", JSON.toJSONString(dPointEntities));
        map.put("dDrawLineEntities", JSON.toJSONString(dDrawLineEntities));
        map.put("dCrossCostEntities", JSON.toJSONString(dCrossCostEntities));
        getNovate(context, "点位信息上传中...").rxPost(Contants.projectPoint, map, new Callback(3, c, d));
    }

    //更新项目信息(设备及材料)
    public static void projectDeviceAndMaterial(Context context, String uuid, NovaCallback c) {
        Map<String, Object> map = getParams(context);
        DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
        List<DMaterialBuyEntity> dMaterialBuyEntities = session.getDMaterialBuyEntityDao().queryBuilder()
                .where(DMaterialBuyEntityDao.Properties.PGuid.eq(uuid)
                        , DMaterialBuyEntityDao.Properties.MBDatastate.notEq("已同步")).list();
        List<DMaterialOldEntity> dMaterialOldEntities = session.getDMaterialOldEntityDao().queryBuilder()
                .where(DMaterialOldEntityDao.Properties.PGuid.eq(uuid)
                        , DMaterialOldEntityDao.Properties.MBDatastate.notEq("已同步")).list();
        List<DDeviceBuyEntity> dDeviceBuyEntities = session.getDDeviceBuyEntityDao().queryBuilder()
                .where(DDeviceBuyEntityDao.Properties.PGuid.eq(uuid)
                        , DDeviceBuyEntityDao.Properties.DBDatastate.notEq("已同步")).list();
        List<DDeviceOldEntity> dDeviceOldEntities = session.getDDeviceOldEntityDao().queryBuilder()
                .where(DDeviceOldEntityDao.Properties.PGuid.eq(uuid)
                        , DDeviceOldEntityDao.Properties.DRDatastate.notEq("已同步")).list();
        map.put("dMaterialBuyEntities", JSON.toJSONString(dMaterialBuyEntities));
        map.put("dMaterialOldEntities", JSON.toJSONString(dMaterialOldEntities));
        map.put("dDeviceBuyEntities", JSON.toJSONString(dDeviceBuyEntities));
        map.put("dDeviceOldEntities", JSON.toJSONString(dDeviceOldEntities));
        getNovate(context, "设备及材料信息上传中...").rxPost(Contants.projectDeviceAndMaterial, map, new Callback(4, c, d));
    }

    //更新项目信息(工程量及工作内容)
    public static void projectCross(Context context, String uuid, NovaCallback c) {
        Map<String, Object> map = getParams(context);
        DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
        List<DOtherCostEntity> dOtherCostEntities = session.getDOtherCostEntityDao().queryBuilder()
                .where(DOtherCostEntityDao.Properties.PGuid.eq(uuid)
                        , DOtherCostEntityDao.Properties.CCDatastate.notEq("已同步")).list();
        List<DMaterialCostEntity> dMaterialCostEntities = session.getDMaterialCostEntityDao().queryBuilder()
                .where(DMaterialCostEntityDao.Properties.PGuid.eq(uuid)
                        , DMaterialCostEntityDao.Properties.MCDatastate.notEq("已同步")).list();
        List<DWorkContentEntity> dWorkContentEntities = session.getDWorkContentEntityDao().queryBuilder()
                .where(DWorkContentEntityDao.Properties.PGuid.eq(uuid)
                        , DWorkContentEntityDao.Properties.WCDatastate.notEq("已同步")).list();
        map.put("dOtherCostEntities", JSON.toJSONString(dOtherCostEntities));
        map.put("dMaterialCostEntities", JSON.toJSONString(dMaterialCostEntities));
        map.put("dWorkContentEntities", JSON.toJSONString(dWorkContentEntities));
        getNovate(context, "工程量及工作内容信息上传中...").rxPost(Contants.projectCross, map, new Callback(5, c, d));
    }

    //更新项目信息(工程量及工作内容)
    public static void updateMaterialModel(Context context, NovaCallback c) {
        Map<String, Object> map = getParams(context);
        DaoSession session = GreenDaoManager.getInstance().getmDaoSession();
        List<DMaterialModelEntity> dMaterialModelEntities = session.getDMaterialModelEntityDao().queryBuilder()
                .where(DMaterialModelEntityDao.Properties.Datastate.notEq("已同步")).list();
        List<DMaterialModelItemEntity> dMaterialModelItemEntities = session.getDMaterialModelItemEntityDao().queryBuilder()
                .where(DMaterialModelItemEntityDao.Properties.Datastate.notEq("已同步")).list();
        map.put("dMaterialModelEntities", JSON.toJSONString(dMaterialModelEntities));
        map.put("dMaterialModelItemEntities", JSON.toJSONString(dMaterialModelItemEntities));
        getNovate(context, "材料模块更新中...").rxPost(Contants.updateMaterialModel, map, new Callback(5, c, d));
    }

}
